﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Linkman
{
    /// <summary>
    /// ListView 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        private int userid;

        private void Page_Load(object sender, EventArgs e)
        {
            userid = SiteContext.Current.UserID;

            if (!Page.IsPostBack)
            {
                Session["cm_permission"] = "administrator";
                int type = (Request.QueryString["type"] == null) ? 1 : Int32.Parse(Request.QueryString["type"]);
                GridBind(type);
            }
        }

        private void GridBind(int type)
        {
            var linkman = new MyLinkman();
            SqlDataReader dr_linkman = linkman.GetMyLinkman(type, userid);
            DataTable dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);
            dt_linkman.TableName = "Linkman";
            var ds = new DataSet();
            ds.Tables.Add(dt_linkman);
            switch (type)
            {
                case 1:
                    dgrd_StaffLinkman.Visible = true;
                    dgrd_ClientLinkman.Visible = false;
                    dgrd_CustomLinkman.Visible = false;

                    td_Staff.Attributes["background"] = "../../images/maillistbutton2.gif";
                    td_Client.Attributes["background"] = "../../images/maillistbutton1.gif";
                    td_Custom.Attributes["background"] = "../../images/maillistbutton1.gif";

                    dgrd_StaffLinkman.DataSource = dt_linkman.DefaultView;
                    dgrd_StaffLinkman.DataKeyField = "Staff_ID";
                    dgrd_StaffLinkman.DataBind();
                    break;
                case 2:
                    dgrd_StaffLinkman.Visible = false;
                    dgrd_ClientLinkman.Visible = true;
                    dgrd_CustomLinkman.Visible = false;

                    td_Staff.Attributes["background"] = "../../images/maillistbutton1.gif";
                    td_Client.Attributes["background"] = "../../images/maillistbutton2.gif";
                    td_Custom.Attributes["background"] = "../../images/maillistbutton1.gif";

                    dgrd_ClientLinkman.DataSource = dt_linkman.DefaultView;
                    dgrd_ClientLinkman.DataKeyField = "ID";
                    dgrd_ClientLinkman.DataBind();
                    break;
                case 3:
                    dgrd_StaffLinkman.Visible = false;
                    dgrd_ClientLinkman.Visible = false;
                    dgrd_CustomLinkman.Visible = true;

                    td_Staff.Attributes["background"] = "../../images/maillistbutton1.gif";
                    td_Client.Attributes["background"] = "../../images/maillistbutton1.gif";
                    td_Custom.Attributes["background"] = "../../images/maillistbutton2.gif";

                    dgrd_CustomLinkman.DataSource = dt_linkman.DefaultView;
                    dgrd_CustomLinkman.DataKeyField = "ID";
                    dgrd_CustomLinkman.DataBind();
                    break;
            }
        }


        private void GridCustomLinkmanByType(int type)
        {
            var linkman = new MyLinkman();
            SqlDataReader dr_linkman = linkman.GetCustomLinkmanByType(type, userid);
            DataTable dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);
            dgrd_CustomLinkman.DataSource = dt_linkman.DefaultView;
            dgrd_CustomLinkman.DataBind();
        }

        private void lbtn_Click(object sender, EventArgs e)
        {
            if (((Control)sender).ID == "lbtn_Custom")
                ddl_CustomLinkmanType.Visible = true;
            else
                ddl_CustomLinkmanType.Visible = false;
            BindTypeList();

            GridBind(Int32.Parse(((LinkButton)sender).CommandArgument));
            switch (Int32.Parse(((LinkButton)sender).CommandArgument))
            {
                case 1:
                    dgrd_StaffLinkman.Visible = true;
                    dgrd_ClientLinkman.Visible = false;
                    dgrd_CustomLinkman.Visible = false;
                    break;
                case 2:
                    dgrd_StaffLinkman.Visible = false;
                    dgrd_ClientLinkman.Visible = true;
                    dgrd_CustomLinkman.Visible = false;
                    break;
                case 3:
                    dgrd_StaffLinkman.Visible = false;
                    dgrd_ClientLinkman.Visible = false;
                    dgrd_CustomLinkman.Visible = true;
                    break;
            }
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            string selectedstring = "";
            int type = 0;
            var mlinkman = new MyLinkman();

            if (dgrd_StaffLinkman.Visible)
            {
                type = 1;
                //得到选中的checkbox的id
                foreach (DataGridItem dgi in dgrd_StaffLinkman.Items) //找到checkbox control
                {
                    for (int i = 0; i < dgi.Cells[0].Controls.Count; i++)
                    {
                        if ((dgi.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox") && ((CheckBox)dgi.Cells[0].Controls[i]).Checked)
                        {
                            selectedstring += dgrd_StaffLinkman.DataKeys[dgi.ItemIndex] + ",";
                        }
                    }
                }
            }
            else if (dgrd_ClientLinkman.Visible)
            {
                type = 2;
                foreach (DataGridItem dgi in dgrd_ClientLinkman.Items) //找到checkbox control
                {
                    for (int i = 0; i < dgi.Cells[0].Controls.Count; i++)
                    {
                        if ((dgi.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox") && ((CheckBox)dgi.Cells[0].Controls[i]).Checked)
                        {
                            selectedstring += dgrd_ClientLinkman.DataKeys[dgi.ItemIndex] + ",";
                        }
                    }
                }
            }
            else if (dgrd_CustomLinkman.Visible)
            {
                type = 3;
                foreach (DataGridItem dgi in dgrd_CustomLinkman.Items) //找到checkbox control
                {
                    for (int i = 0; i < dgi.Cells[0].Controls.Count; i++)
                    {
                        if ((dgi.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox") && ((CheckBox)dgi.Cells[0].Controls[i]).Checked)
                        {
                            selectedstring += dgrd_CustomLinkman.DataKeys[dgi.ItemIndex] + ",";
                        }
                    }
                }
            }

            if (selectedstring.IndexOf(",") != -1)
            {
                selectedstring = selectedstring.Substring(0, selectedstring.Length - 1);
                string[] arrselected = selectedstring.Split(',');
                for (int i = 0; i < arrselected.Length; i++)
                {
                    if (arrselected[i].Trim() != "")
                    {
                        mlinkman.DelLinkmanFromList(type, Int32.Parse(arrselected[i]));
                    }
                }
                GridBind(type);
            }
        }

        //bound to customlinkmantype
        private void BindTypeList()
        {
            using (var dr = MyLinkman.GetCustomLinkmanType())
            {
                ddl_CustomLinkmanType.DataSource = dr;
                ddl_CustomLinkmanType.DataValueField = "ID";
                ddl_CustomLinkmanType.DataTextField = "type";
                ddl_CustomLinkmanType.DataBind();
            }
            ddl_CustomLinkmanType.Items.Insert(0, new ListItem("全部", "all"));
        }

        private void dgrd_StaffLinkman_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
            GridBind(1);
        }

        private void dgrd_ClientLinkman_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
            GridBind(2);
        }

        private void dgrd_CustomLinkman_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
            if (ddl_CustomLinkmanType.SelectedItem.Value == "all")
                GridBind(3);
            else
                GridCustomLinkmanByType(Int32.Parse(ddl_CustomLinkmanType.SelectedItem.Value));
        }

        private void ddl_CustomLinkmanType_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgrd_CustomLinkman.CurrentPageIndex = 0;
            if (((DropDownList)sender).SelectedItem.Value != "all")
                GridCustomLinkmanByType(Int32.Parse(ddl_CustomLinkmanType.SelectedItem.Value));
            else
                GridBind(3);
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.ddl_CustomLinkmanType.SelectedIndexChanged += new System.EventHandler(this.ddl_CustomLinkmanType_SelectedIndexChanged);
            this.btn_Del.Click += new System.EventHandler(this.btn_Del_Click);
            this.lbtn_Staff.Click += new System.EventHandler(this.lbtn_Click);
            this.lbtn_Client.Click += new System.EventHandler(this.lbtn_Click);
            this.lbtn_Custom.Click += new System.EventHandler(this.lbtn_Click);
            this.dgrd_CustomLinkman.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgrd_CustomLinkman_PageIndexChanged);
            this.dgrd_ClientLinkman.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgrd_ClientLinkman_PageIndexChanged);
            this.dgrd_StaffLinkman.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgrd_StaffLinkman_PageIndexChanged);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}